package com.robot.service.action_unit;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.robot.common.enums.ActionUnitTypeEnum;
import com.robot.common.enums.IsDeleteEnum;
import com.robot.common.enums.KnowledgeTypeEnum;
import com.robot.common.utils.MinioUtils;
import com.robot.dao.entity.Knowledge;
import com.robot.dao.entity.RobotKnowledge;
import com.robot.dao.entity.UserKnowledge;
import com.robot.dao.mapper.ActionUnitMapper;
import com.robot.dao.mapper.KnowledgeMapper;
import com.robot.dao.mapper.RobotKnowledgeMapper;
import com.robot.dao.vo.FileOverViewVO;
import com.robot.dao.vo.KnowledgeDetailVO;
import com.robot.service.UserKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
// 智能体组件：知识库。相关依赖：
// 1.知识库表
// 2.actionunit表
// 3.minio组件
// 4.智能体-知识库表
// 5.文件列表
// 组件核心方法：create、update、run。
@Component
public class ActionUnitImplKnowledge implements ActionUnit{
    @Autowired
    private ActionUnitHelper actionUnitHelper;
    @Autowired
    private KnowledgeMapper knowledgeMapper;
    @Autowired
    private MinioUtils minioUtils;
    @Autowired
    private RobotKnowledgeMapper robotKnowledgeMapper;
    @Autowired
    private UserKnowledgeService userKnowledgeService;

    // func执行
    public void run(){
    }

    public ActionUnitImplKnowledge(){

    }

    public  void CreateThis(Long robotId,String robotName,Long createUserId){
        // 创建知识库-ActionUnit
        com.robot.dao.entity.ActionUnit knowledgeActionUnit = new com.robot.dao.entity.ActionUnit();
        knowledgeActionUnit.setActionUnitType(ActionUnitTypeEnum.DATA_SOURCE.getCode());
        knowledgeActionUnit.setRobotId(robotId);
        knowledgeActionUnit.setUnitName(robotName + "知识库");
        String jsonString = JSONObject.toJSONString(new KnowledgeDetailVO());
        knowledgeActionUnit.setDetail(jsonString);
        knowledgeActionUnit.setCreateTime(LocalDateTime.now());
        knowledgeActionUnit.setUpdateTime(LocalDateTime.now());
        knowledgeActionUnit.setUnitTemplateCode("knowledge");
        this.actionUnitHelper.CreateActionUnit(knowledgeActionUnit);

        // 创建知识库-mysql
        Knowledge knowledge = new Knowledge();
        knowledge.setKnowledgeType(KnowledgeTypeEnum.INDIVIDUAL.getCode());
        knowledge.setIsDelete(IsDeleteEnum.EXIST.getValue());
        knowledge.setKnowledgeName(robotName + "知识库");
        knowledge.setApplicationNumber(1);
        knowledge.setFileNumber(0);
        String bucketName = minioUtils.createBucket();
        knowledge.setMinioBucketName(bucketName);
        knowledgeMapper.create(knowledge);

        // 创建批量上传文件夹
        Knowledge batchUploadSet = new Knowledge();
        batchUploadSet.setKnowledgeType(KnowledgeTypeEnum.BATCH_UPLOAD.getCode());
        batchUploadSet.setIsDelete(IsDeleteEnum.EXIST.getValue());
        batchUploadSet.setKnowledgeName(robotName + "批量上传文件夹");
        batchUploadSet.setApplicationNumber(0);
        batchUploadSet.setFileNumber(0);
        String batchUploadSetBucketName = minioUtils.createBucket();
        batchUploadSet.setMinioBucketName(batchUploadSetBucketName);
        knowledgeMapper.create(batchUploadSet);


        //创建机器人-知识库关系实体
        RobotKnowledge robotKnowledge = new RobotKnowledge();
        robotKnowledge.setKnowledgeId(knowledge.getId());
        robotKnowledge.setRobotId(robotId);
        robotKnowledgeMapper.create(robotKnowledge);

        UserKnowledge userKnowledge = new UserKnowledge();
        userKnowledge.setKnowledgeId(knowledge.getId());
        userKnowledge.setUserId(createUserId);
        userKnowledgeService.save(userKnowledge);

        UserKnowledge userKnowledgeBatchUploadSet = new UserKnowledge();
        userKnowledgeBatchUploadSet.setKnowledgeId(batchUploadSet.getId());
        userKnowledgeBatchUploadSet.setUserId(robotId);
        userKnowledgeService.save(userKnowledgeBatchUploadSet);
    }

    // 有参数的build

    // 各个参数get
}
